package com.xueyi.aixihu.config;

import com.xueyi.aixihu.exception.SzException;
import lombok.SneakyThrows;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 项目名称：caimi-admin
 * 类 名 称：SpringContentConfig
 * 类 描 述：Spring容器Config
 * 创建时间：2020/8/5 9:59 上午
 * 创 建 人：俞烨-company-mac
 */
@Component
public class SpringContextConfig implements BeanPostProcessor, BeanFactoryPostProcessor {

    /***
     * Spring上下文容器
     */
    private static ConfigurableApplicationContext SPRING_CONTENT = null;

    public static ConfigurableListableBeanFactory BEAN_FACTORY = null;

    public static void setSpringContent(ConfigurableApplicationContext content) throws SzException {
        SPRING_CONTENT = content;
        BEAN_FACTORY = content.getBeanFactory();

        content.getBeanFactory();
//        content.getBean(TimerTask.class).mondayClearExam();
//        content.getBean(TimerTask.class).mondayAutoCreateExam();
    }

    public static <T> T getBean(Class<T> clazz) {
        return SPRING_CONTENT != null ? SPRING_CONTENT.getBean(clazz) : BEAN_FACTORY.getBean(clazz);
    }


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        SpringContextConfig.BEAN_FACTORY = configurableListableBeanFactory;
    }
}
